package com.whmall.service.crm.module.supplierdeveloppool.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.entity.dto.supplierdevelop.SupplierDevelopPoolLogQueryRspDTO;
import com.whmall.service.crm.entity.dto.supplierdevelop.SupplierDevelopPoolLogSaveReqDTO;
import com.whmall.service.crm.module.supplierdeveloppool.dao.SupplierDevelopPoolLogMapper;
import com.whmall.service.crm.module.supplierdeveloppool.entity.SupplierDevelopPoolLogDO;
import com.whmall.service.crm.module.supplierdeveloppool.entity.qo.SupplierDevelopPoolLogQO;
import com.whmall.service.crm.module.supplierdeveloppool.service.SupplierDevelopPoolLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SupplierDevelopPoolLogServiceImpl implements SupplierDevelopPoolLogService {

    @Autowired
    private SupplierDevelopPoolLogMapper poolLogMapper;

    @Autowired
    private SupplierDevelopPoolLogValidator validator;

    @Override
    public Long addLog(SupplierDevelopPoolLogSaveReqDTO logSaveReqDTO) {

        validator.validate(logSaveReqDTO);
        SupplierDevelopPoolLogDO developPoolLogDO = new SupplierDevelopPoolLogDO();
        BeanUtils.copyProperties(logSaveReqDTO, developPoolLogDO);
        developPoolLogDO.setHandleTime(new Date());
        poolLogMapper.insertSelective(developPoolLogDO);
        return developPoolLogDO.getId();
    }

    @Override
    public PageRspDTO<SupplierDevelopPoolLogQueryRspDTO> list(PageReqDTO pageReqDTO, Long companyId) {

        Assert.notNull(companyId, "公司id不能为空");

        //查询结果
        PageInfo<SupplierDevelopPoolLogDO> pageInfo = PageHelper.startPage(pageReqDTO.getPageNum(),
                pageReqDTO.getPageSize()).doSelectPageInfo(() -> poolLogMapper.listBy(
                SupplierDevelopPoolLogQO.builder().companyId(companyId).build()));
        if (CollUtil.isEmpty(pageInfo.getList())){
            PageRspDTO<SupplierDevelopPoolLogQueryRspDTO> pageRspDTO = new PageRspDTO<>();
            pageRspDTO.setPageSize(pageReqDTO.getPageSize());
            pageRspDTO.setPageNum(pageReqDTO.getPageNum());
            return pageRspDTO;
        }
        //转换参数
        List<SupplierDevelopPoolLogQueryRspDTO> queryRspDTOS = pageInfo.getList().stream().map(e -> {
            SupplierDevelopPoolLogQueryRspDTO queryRspDTO = new SupplierDevelopPoolLogQueryRspDTO();
            BeanUtils.copyProperties(e, queryRspDTO);
            return queryRspDTO;
        }).collect(Collectors.toList());
        return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(),
                pageInfo.getPages(), (int) pageInfo.getTotal(), queryRspDTOS);
    }
}
